<template>
	<up-popup :show="show" :round="12" customStyle="bottom:106rpx">
		<view class="container_pop">
			<view class="common-car">
				<view class="empty-shop-car" v-if="isEmpty">
					<image src="../../static/empty_shop_car.png" class="empty-shop-car-image" mode=""></image>
					<text style="color: #333333;">当前您的购物车是空的</text>
				</view>
				<view class="shop-car" v-else>
					<view class="shop-car-header">
						<view class="statistics-left" v-if="statisticsIndex" @tap="allCheck">
							<image src="../../static/select.png" class="checked-image" mode="">
							</image>
							&nbsp;&nbsp;<text>全选</text>&nbsp;
						</view>
						<view class="statistics-left" v-else @tap="allCheck">
							<image src="../../static/not_select.png" class="checked-image" mode="">
							</image>
							&nbsp;&nbsp;<text>全选</text>&nbsp;
						</view>
						<view class="clean" @click="clean"> <up-icon name="trash" color="#6A6967"></up-icon>清空购物车</view>
					</view>
					<scroll-view scroll-y="true" style="height: 700rpx;overflow: auto;">
						<view class="store-box" v-for="(itemw,indexw) in datas" :key="indexw">
							<view class="goodsInfo">
								<image src="../../static/select.png" v-if="itemw.checked == 2" class="checked-image"
									mode="" @tap="goodsCheck(indexw,itemw.checked)"></image>
								<image src="../../static/not_select.png" v-else class="checked-image" mode=""
									@tap="goodsCheck(indexw,itemw.checked)"></image>
								<view class="goodsInfo-right">
									<image :src="itemw.goods.main_image" class="goods-image" mode=""></image>
									<view class="goodsInfo-box">
										<text class="goods-name">{{itemw.goods.goods_name}}</text>
										<text class="spe">{{itemw.sku.attr}}</text>
										<view class="goods-box">
											<text class="goods-price">¥{{itemw.sku.price}}</text>
											<view class="goods-num-box">
												<view class="goods-image-sub" @tap="sub(indexw,itemw.number)">
													-
												</view>
												<view class="goods-num">
													{{itemw.num}}
												</view>
												<view class="goods-image-add" @tap="add(indexw,itemw.number)">
													+
												</view>
											</view>
										</view>
									</view>
								</view>
							</view>
						</view>
					</scroll-view>
				</view>
				<slot></slot>
			</view>
		</view>
		<view class="cart">
			<view class="cart-button">
				<l-ball ref="ballRef">
					<template #default="{active}">
						<view class="cart-icon" @click="closeCar">
							<image class="img" :class="{active}" mode="widthFix" src="@/static/Vector.png">
							</image>
							<view class="badge">
								<up-badge bgColor="#F03717" max="99" :value="num"></up-badge>
							</view>
						</view>
					</template>
				</l-ball>
			</view>
			<view class="cart-prices">
				<view class="price">
					<view class="payableAmount">
						¥<text class="text">{{total || 0}}</text>
					</view>
					<!-- <view class="totalAmount">
						<text class="text">预估配送费￥300</text>
					</view> -->
				</view>
			</view>
			<view class="cart-preview" @tap="accounts">去结算</view>
		</view>
	</up-popup>

</template>

<script>
	import {
		http
	} from 'uview-plus'
	export default {
		data() {
			return {
				show: false,
				isEmpty: true,
				iPhoneX: false,
				datas: {},
				statisticsIndex: true,
				total: 0,
				isCut: true,
				list: []
			};
		},
		methods: {
			async setCar() {
				await http.get('/shop/user/cart', {
					header: {
						'Content-Type': 'multipart/form-data'
					}
				}).then(res => {
					this.list = res.items

					this.list.map(item => {
						item.checked = 2
					})
				});
				this.$emit('changeNum');
				this.show = !this.show;
				if (this.list.length == 0) {
					this.isEmpty = true
				} else {
					this.datas = this.list
					this.isEmpty = false
				}
				this.calcTotal();
				
			},
			//清空购物车
			async clean(){
				var arr=[]
				this.datas.find((item,index)=>{
					arr.push(item.id)
				})
				http.post('/shop/user/cart/delete',{user_cart_ids:arr}).then(res=>{
					uni.showToast({
						title:'删除成功',
						icon:'none'
					})
					this.setCar()
				})
			},
			//商品选择
			goodsCheck(goodsIndex, goodsChecked) {
				if (goodsChecked == 1) {
					this.datas[goodsIndex].checked = 2
				} else {
					this.datas[goodsIndex].checked = 1
				}

				//判断是否全选
				let statisticsIndex = true
				this.datas.find((item, index) => {
					if (item.checked == 1) {
						statisticsIndex = false
					}
				})
				if (statisticsIndex == false) {
					this.statisticsIndex = false
				} else {
					this.statisticsIndex = true
				}
				this.calcTotal();

			},

			//减少
			async sub(goodsIndex, goodsnum) {
				if (goodsnum == 1) {
					uni.showToast({
						title: '宝贝数量不能再减少了',
						icon: 'none'
					})
					return
				} else {
					this.datas[goodsIndex].num--
				}
				await http.post('/shop/user/cart/update', {
					user_cart_id: this.datas[goodsIndex].id,
					num: this.datas[goodsIndex].num
				}).then(res => {
					getApp().get_shopping_List()
					this.calcTotal();
				});
			},
			//增加
			async add(goodsIndex, goodsnum) {
				this.datas[goodsIndex].num++
				await http.post('/shop/user/cart/update', {
					user_cart_id: this.datas[goodsIndex].id,
					num: this.datas[goodsIndex].num
				}).then(res => {
					getApp().get_shopping_List()
					this.calcTotal();
				});
			},
			//计算总价
			calcTotal() {
				let total = 0
				this.datas.find((item, index) => {
					if (item.checked == 2) {
						total = total + item.sku.price * item.num
					}
				})
				this.total = total.toFixed(2)
			},
			//全选
			allCheck() {
				if (this.statisticsIndex) {
					this.datas.find((item, index) => {
						item.checked = 1
					})
					this.statisticsIndex = false
				} else {
					this.datas.find((item, index) => {
						item.checked = 2
					})
					this.statisticsIndex = true
				}
				this.calcTotal();
			},
			closeCar() {
				this.show = false
				this.$emit('changeNum');
			},
			judgeSelect(){
				let judge = false
				this.datas.find((item,index)=>{
					if(item.checked == 2){
						judge = true
					}
				})
				return judge
			},
			accounts(){
				let judge = this.judgeSelect()
				let arr = []
				if(judge){
					this.datas.find((item,index)=>{
						if(item.checked == 2){
							arr.push(item)
						}
					})
					uni.setStorageSync('orderInfo',arr)
					uni.navigateTo({
						url:"/pages/order/creatOrder"
					})
				}else{
					uni.showToast({
						title:'您当前未选择任何商品,结算失败',
						icon:'none'
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container_pop {
		width: 100%;
		height: 800rpx;
		padding: 32rpx;
		box-sizing: border-box;
		position: relative;
		overflow: hidden;
	}

	.common-car {
		width: 100%;
		min-height: 100vh;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.empty-shop-car {
		width: 100%;
		min-height: 680rpx;
		display: flex;
		flex-direction: column;
		align-items: center;

		.empty-shop-car-image {
			width: 340rpx;
			height: 278rpx;
			margin-top: 102rpx;
		}

		text {
			margin-top: 40rpx;
			font-size: 28rpx;
			font-weight: 400;
			color: #fff;
		}

		.empty-shop-car-btn {
			width: 240rpx;
			height: 90rpx;
			background: $main-color;
			border-radius: 200rpx;
			margin-top: 40rpx;
			text-align: center;
			line-height: 90rpx;
			font-size: 30rpx;
			font-weight: 400;
			color: #000;
		}
	}

	.shop-car {
		width: 750rpx;
		display: flex;
		flex-direction: column;
		align-items: center;

		.shop-car-header {
			width: 690rpx;
			height: 80rpx;
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-between;

			text {
				font-size: 28rpx;
				font-weight: 400;
				color: #313133;
			}

			.clean {
				display: flex;
				font-size: 24rpx;
				color: #6A6967;
			}
		}

		.store-box {
			width: 686rpx;
			padding: 20rpx 0rpx;
			box-sizing: border-box;
			margin: auto;
			display: flex;
			flex-direction: column;
			align-items: center;
			background-color: #FFFFFF;
			border-bottom: 1px solid #EAEAEA;

			.store-header {
				width: 690rpx;
				height: 78rpx;
				padding: 0 30rpx;
				display: flex;
				flex-direction: row;
				align-items: center;
				font-size: 28rpx;
				font-weight: 400;
				color: #313133;

				.checked-image {
					width: 36rpx;
					height: 36rpx;
				}

				text {
					font-size: 28rpx;
					font-weight: 400;
					color: #313133;
					margin-left: 20rpx;
				}

				.label {
					width: 14rpx;
					height: 24rpx;
					margin-left: 10rpx;
					margin-top: 5rpx;
				}
			}

			.goodsInfo {
				width: 690rpx;

				display: flex;
				flex-direction: row;
				align-items: center;
				border-bottom: 2rpx solid #EDEDED;
				overflow: hidden;

				&:nth-last-child(1) {
					border: none;
				}

				.checked-image {
					width: 36rpx;
					height: 36rpx;
				}

				.goodsInfo-right {
					width: 634rpx;
					margin-left: 20rpx;
					display: flex;
					flex-direction: row;
					align-items: center;

					.goods-image {
						width: 106rpx;
						height: 106rpx;
					}

					.goods-image-sub {
						width: 42rpx;
						height: 42rpx;
						border-radius: 22rpx;
						opacity: 1;
						line-height: 42rpx;
						border: 2rpx solid #08AFFE;
						text-align: center;
						box-sizing: border-box;
					}

					.goods-image-add {
						width: 42rpx;
						height: 42rpx;
						line-height: 42rpx;
						border-radius: 22rpx;
						opacity: 1;
						background: #08AFFE;
						text-align: center;
						color: #fff
					}

					.goodsInfo-box {
						width: 100%;
						margin-left: 24rpx;
						display: flex;
						flex-direction: column;

						.goods-name {
							width: 428rpx;
							font-size: 28rpx;
							font-weight: 500;
							color: #313133;
						}

						.spe {
							width: 100%;
							margin-top: 10rpx;
							font-size: 22rpx;
							font-weight: 400;
							color: #9C9C9C;
						}

						.goods-box {
							width: 100%;
							margin-top: 18rpx;
							display: flex;
							flex-direction: row;
							align-items: center;
							justify-content: space-between;

							.goods-price {
								font-size: 28rpx;
								font-weight: 500;
								color: #FD4E24;
							}

							.goods-num-box {
								height: 46rpx;
								display: flex;
								flex-direction: row;
								align-items: center;

								.goods-image {
									width: 46rpx;
									height: 44rpx;
									text-align: center;
									line-height: 44rpx;
									border: 1rpx solid #CFCFCF;
									font-size: 38rpx;
								}

								.goods-num {
									width: 60rpx;
									height: 44rpx;
									text-align: center;
									line-height: 44rpx;
									font-size: 33rpx;
									font-weight: 400;
									color: #666666;
								}
							}
						}
					}
				}
			}
		}

	}

	.statistics-left {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		font-size: 24rpx;
		color: #868789;

		image {
			width: 36rpx;
			height: 36rpx;
		}

		text {
			font-size: 28rpx;
			font-weight: 500;
			color: #3D3D3D
		}
	}
</style>